<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/留言板.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        a {
            color: inherit;
            text-decoration: none;
        }

        .commentBar {
            width: 600px;
            margin: 0 auto;
        }

        .commentBar .commentSendBox {
            margin-top: 50px;
        }

        .commentBar .commentSendBox textarea {
            width: 100%;
            height: 200px;
            resize: none;
            padding: 10px;
            box-sizing: border-box;
            outline: none;
        }

        .commentBar .commentSendBox .sendBox {
            display: flex;
            justify-content: space-between;
        }

        .commentListBox {
            margin-top: 50px;
            font-size: 12px;
            line-height: 18px;
        }

        .showComment li {
            padding: 10px;
        }

        .showComment li+li {
            border-top: 1px dashed #333;
        }

        .comment {
            text-indent: 10px;
        }

        .commentDetail {
            display: flex;
            text-indent: 10px;
        }

        .reply {
            margin-left: 50px;
            cursor: context-menu;
        }

        .replyList {
            padding-left: 50px;
        }

        .replyList .comment-header {
            margin-top: 12px;
        }

        .comment-bottom {
            height: 80px;
            display: flex;
            margin-top: 20px;
            display: none;
        }

        .comment-bottom .replyCon {
            flex: 1;
            height: 80px;
            border-radius: 10px;
            margin-right: 10px;
            outline: none;
            padding: 10px;
            box-sizing: border-box;
            resize: none;
        }

        .comment-bottom .replyBtn {
            width: 80px;
            height: 80px;
            background-color: #00b5e5;
            border-radius: 10px;
            word-break: break-all;
            font-size: 24px;
            word-spacing: 10px;
            line-height: 40px;
            text-align: center;
            cursor: context-menu;
        }
    </style>
</head>

<body>
    <div class="commentBar">
        <div class="commentSendBox">
            <textarea name="" id="" cols="30" rows="10" class="commentCon"></textarea>
            <div class="sendBox">
                <div class="commentTips">您还可以输入50个字</div>
                <button class="commentSend">发送</button>
            </div>
        </div>
        <div class="commentListBox">
            <ul class="showComment">
                <!-- <li>
                    <div class="comment-header">
                        <div class="nick">PigZhang</div>
                        <div class="comment">天对地，雨对风，黑心棉对白岩松</div>
                        <div class="commentDetail">
                            <div class="time">2021-05-16 18:03</div>
                            <div class="reply">回复</div>
                        </div>
                    </div>
                    <div class="replyList">
                        <div class="comment-header">
                            <div class="nick">PigZhang</div>
                            <div class="comment">天对地，雨对风，黑心棉对白岩松</div>
                            <div class="commentDetail">
                                <div class="time">2021-05-16 18:03</div>
                                <div class="reply">回复</div>
                            </div>
                        </div>
                    </div>
                    <div class="comment-bottom">
                        <textarea class="replyCon" name="" id="" cols="30" rows="10"></textarea>
                        <a class="replyBtn" href="javascript:;">发表 评论</a>
                    </div>
                </li> -->
            </ul>
        </div>
    </div>
</body>
<script>
    var commentCon = document.getElementsByClassName("commentCon")[0];
    var commentSend = document.getElementsByClassName("commentSend")[0];
    var commentTips = document.getElementsByClassName("commentTips")[0];
    var showComment = document.getElementsByClassName("showComment")[0];

    commentCon.onkeypress = commentCon.onkeyup = inputHandler;
    commentSend.onclick = enterHandler;

    showComment.onclick = function (e) {
        var e = e || window.event;
        var target = e.target || e.srcElement;
        // 点击回复
        if (target.className == "reply") {
            var replayList = showComment.getElementsByClassName("comment-bottom");
            for (var i = 0; i < replayList.length; i++) {
                replayList[i].style.display = "none";
            }
            var parentLi = parent(parent(parent(target)));
            var commentBtn = parentLi.getElementsByClassName("comment-bottom")[0];
            commentBtn.style.display = "flex";
            
        }

        //回复发送
        if (target.className == "replyBtn") {
            replyHandler(target);
        }
    }

    //回复发送的方法
    function replyHandler(target) {
        var replyCon = prev(target).value;

        replyCon = sensitiveWords(replyCon);

        var li = document.createElement("li");
        li.innerHTML =
                        `<div class="comment-header">
                            <div class="nick">PigZhang: <span class="comment">${replyCon}</span></div>
                            <div class="commentDetail">
                                <div class="time">${dateFormat("YY-MM-DD hh:mm")}</div>
                            </div>
                        </div>`;
        var replayLi = prev(parent(target));
        replayLi.append(li);
        prev(target).value = "";
        parent(target).style.display = "none";
    }

    // 输入框的方法
    function inputHandler(e) {
        var e = e || window.event;
        var comment = commentCon.value;
        var maxChar = 50; //规定能输入的字数
        var num = maxChar - comment.length;
        commentTips.textContent = `您还可以输入${num}个字`;
        if (num <= 0) {
            // 如果num=0，阻止浏览器的默认行为(阻止输入)
            if (e.preventDefault) {
                e.preventDefault();
            } else {
                e.returnValue = false;
            }
            var str = comment.slice(0, maxChar); //中文还能输入，所以只裁切规定的字数
            commentCon.value = str.trim(); //再重新赋值，并去除两边空格
        }
        //快捷键发送
        var keyCode = e.which || e.keyCode;
        if (e.ctrlKey && keyCode == 13) enterHandler();
    }

    //敏感词过滤
    function sensitiveWords(content) {
        var badWordList = ["垃圾", "菜鸡", "cnm", "辣鸡", "菜b", "菜", "艹", "操"];
        // 敏感词过滤
        var badStr = badWordList.join("|"); //将敏感词转换成正则里或的字符串
        var reg = new RegExp(badStr, "ig"); //构造函数生成一个包含敏感词的规则
        //如果含有敏感词就替换成*(有一个替换一个)
        return content.replace(reg, function (badWord) {
            // console.log(badWord);
            var newStr = "";
            for (var i = 0; i < badWord.length; i++) {
                newStr += "*";
            }
            return newStr;
        });
    }

    // 发送的方法
    function enterHandler() {
        var comment = commentCon.value;
        if (comment) {

            // 传入内容
            comment = sensitiveWords(comment);
            // console.log(sensitiveWords(comment));

            var li = document.createElement("li");
            li.innerHTML =
                    `<div class="comment-header">
                        <div class="nick">PigZhang</div>
                        <div class="comment">${comment}</div>
                        <div class="commentDetail">
                            <div class="time">${dateFormat("YY-MM-DD hh:mm")}</div>
                            <div class="reply">回复</div>
                        </div>
                    </div>
                    <div class="replyList"></div>
                    <ul class="comment-bottom">
                        <textarea class="replyCon" name="" id="" cols="30" rows="10"></textarea>
                        <a class="replyBtn" href="javascript:;">发表 评论</a>
                    </ul>`

            showComment.prepend(li);
            commentCon.value = "";

            // 假禁止
            // commentCon.disabled = true;
            // commentSend.disabled = true;

            // 真禁止
            commentCon.onkeypress = commentCon.onkeyup = null;
            commentSend.onclick = null;

            var count = 10;
            commentTips.textContent = `距离下一次输入还剩${count}秒`;
            var timer = setInterval(function () {
                count--;
                commentTips.textContent = `距离下一次输入还剩${count}秒`;
                if (count <= 0) {
                    clearInterval(timer);
                    commentTips.textContent = `您还可以输入50个字`;

                    // commentCon.disabled = false;
                    // commentSend.disabled = false;

                    // 解禁
                    commentCon.onkeypress = commentCon.onkeyup = inputHandler;
                    commentSend.onclick = enterHandler;
                }
            }, 1000);
        }
    }

</script>

</html>